<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

//ajax 格式是输出执行数据$date返回数据,$ydate返回你给的数据,$code原因代码默认不返回传输数据，还有成功200
	function reajax($date='',$msg='',$code='200'){
	 	  $arr['date']=$date;
	 	  $arr['msg']=$msg;
	 	  $arr['code']=$code;
	 	  return json_encode($arr);
	 	  exit;
	 }

  //随机生成邀请码 
	function getRandomString($prefixLength = 2, $suffixLength = 4){
    $t = '';
    for ($i=0; $i<$prefixLength; $i++) {
        $salt = mt_rand(1000, 9999);
        if ($salt % 2) {
            $t .= chr(mt_rand(97,122));
        } else {
            $t .= chr(mt_rand(65,90));
        }
    }
    $min = intval(str_pad('1', $suffixLength, '0', STR_PAD_RIGHT));
    $max = intval(str_repeat('9', $suffixLength));
    return $t . mt_rand($min, $max);
   } 

  //后台用户操作日志 
  function admin_log($content){
          $log['admin_id'] = session('admin_id');
          $log['log_time'] = date('Y-m-d H:i:s');
          $log['loginfo'] = session('user_name').$content;
          $log['ip'] = request()->ip(); 
          $location = new location\IpLocation();
          $ips = $location->getlocation($log['ip']);
          $log['ipadress'] = $ips['country'];
          $logs = db('admin_log')->insertGetId($log);
   }
  
  //清除缓存
  function rmdirs($dir){
        $dir_arr = scandir($dir);
        foreach($dir_arr as $key=>$val){
            if($val == '.' || $val == '..'){}
            else {
                if(is_dir($dir.'/'.$val))    
                {                            
                    if(@rmdir($dir.'/'.$val) == 'true'){}    //去掉@您看看                
                    else
                    rmdirs($dir.'/'.$val);                    
                }
                else                
                unlink($dir.'/'.$val);
            }
        }
    }

  //函数：格式化字节大小
  function format_bytes($size, $delimiter = ''){
    $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    for ($i = 0; $size >= 1024 && $i < 5; $i++) {
        $size /= 1024;
    }
    return round($size, 2) . $delimiter . $units[$i];
  }  



